<?PHP
//require_once 'Mail.php';
require_once APP_ROOT.'model/common/user.php';
require_once APP_ROOT.'model/common/article.php';
require_once APP_ROOT.'model/common/utils.php';

require_once WACT_ROOT . 'util/dataspace.inc.php';
require_once APP_ROOT.'include/pear/Mail.php';
require_once APP_ROOT.'include/pear/Mail/mime.php';
require_once 'model/common/date.php';
require_once APP_ROOT.'model/common/site.php';

class Sportmail {

	function Sportmail() {
	}

	function sendRegisterMail($user_id) {
		$site_id = Site::getSiteID();
		$user=&User::getRecord($user_id);
		$login=$user->get('login');
		$password=$user->get('password');

		$to=$user->get('email');
		$site = "New York City Sporting News Site";
		if ($site_id == 1) $site = "Chicago Sporting News Site";

		$from = "support@nycsportingnews.com";
		if ($site_id == 1) $from = "support@chicagosportingnews.com";

		$subject="Registration on ".$site;

        $body="Thank you for joining our ".$site.".\n";
		$body.=" Your login information is: \n";
		$body.="Username: ".$login."\n";
		$body.="Password: ".$password."\n";
		$body.="You profile is on http://".$_SERVER['HTTP_HOST'].'/myprofile/index.htm';

        $myheader = "Return-Path: ".$from."\n";
		$myheader .= "X-Sender: ".$from."\n";
		$myheader .= "From: Sporting News <".$from.">\n";
		$myheader .= "X-Mailer:PHP 4.3\n";
		$myheader .= "MIME-Version: 1.0\n";

		@ini_set(sendmail_from, $from);

		if(mail($to,$subject,$body,$myheader)) {
			error_log("[MAIL][TO: $to]");
		}

	}

	function sendPswdreminderMail($to,$login,$password) {
		$site_id = Site::getSiteID();
        $site = "New York City Sporting News Site";
		if ($site_id == 1) $site = "Chicago Sporting News Site";

    	$from = "support@nycsportingnews.com";
		if ($site_id == 1) $from = "support@chicagosportingnews.com";

		$subject="Remind your password on ".$site;
		$body="Your login information on our ".$site." is:\n";
		$body.="Username: ".$login."\n";
		$body.="Password: ".$password."\n";

        $myheader = "Return-Path: ".$from."\n";
		$myheader .= "X-Sender: ".$from."\n";
		$myheader .= "From: Sporting News <".$from.">\n";
		$myheader .= "X-Mailer:PHP 4.3\n";
		$myheader .= "MIME-Version: 1.0\n";

		@ini_set(sendmail_from, $from);

		if(mail($to,$subject,$body,$myheader)) {
			error_log("[MAIL][TO: $to]");
		}
	}


	function sendArticle(&$responseModel) {
        $site_id = Site::getSiteID();
        $site = "New York City Sporting News Site";
		if ($site_id == 1) $site = "Chicago Sporting News Site";

       	$id=$responseModel->get('article_id');
		$name=$responseModel->get('name');
		$type=$responseModel->get('type');
		$email=$responseModel->get('email');
		$recepients=$responseModel->get('recepients');
		$tomyself=$responseModel->get('tomyself');
		$text=$responseModel->get('permessage');

		$articles=&Article::getRecord($id);
		$headline=$articletext=$articles->get('headline');

		$datestr=$articles->get('date_added');
		$mydate=strtotime($datestr);
        $tz =& new MyTimeZone();
        $mydate = $tz->timeGMT($mydate);
		$datestr = gmstrftime('%b %d, %Y',$mydate);

		$author='<h2>'.$articles->get('firstname').' '.$articles->get('lastname').' - '.$articles->get('title').'</h2>';

		$articletext=$datestr.'<br><h1>'.$headline.'</h1>'.$author.'<hr size=3>';

		$mime=new Mail_mime;
		//if($text) $mime->setTXTBody($text);

		if($type==1) {
			$img=Utils::createEmailArticle($articles->get('description'));
			$art=$articles->get('description');
		}else {
			$img=Utils::createEmailArticle($articles->get('teaser'));
			$art=$articles->get('teaser');
		}

		$art=$img[count($img)-1];
		error_log($art);
		$articletext=$text.'<br>'.$articletext.$art.$name;
		$mime->setHTMLBody($articletext);



		foreach($img as $index=>$item){
			if($index!=(count($img)-1)){

				if($item[0]!='h'){
				$ar=explode('.',$item);
				if($item[0]=='/') $item=substr($item,1);
				$mime->addHTMLImage($item,'image/'.$ar[count($ar)-1]);
				}
			}
		}



		//$mime->addHTMLImage('images/articlesindex.jpg','image/jpeg');

		$headers['From']=$email;
		$headers['Subject']="Article from ".$site."";



		$body=$mime->get()."\n".$name;
		$headers=$mime->headers($headers);
//		error_log("[SEND:]$body");

		$to=$recepients;
		if($tomyself) $to.=','.$email;
		$message =& Mail::factory('mail');
		$message->send($to,$headers,$body);







	}
}
?>
